package com.wyd.wenyongdaAdmin.xo.admin.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.dto.RoleUserDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.entity.RoleUserEntity;
import com.wyd.wenyongdaAdmin.xo.admin.role.mapper.RoleUserMapper;
import com.wyd.wenyongdaAdmin.xo.admin.role.service.IRoleUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUserEntity> implements IRoleUserService {

    private static final Logger log = LoggerFactory.getLogger(RoleUserServiceImpl.class);

    @Override
    public Integer addRoleUser(RoleUserDTO roleUserDTO) {
        if (ObjectUtil.isNotNull(roleUserDTO)) {
            RoleUserEntity roleUser = new RoleUserEntity();
            BeanUtil.copyProperties(roleUserDTO, roleUser);
            return this.baseMapper.insert(roleUser);
        } else {
            return 0;
        }
    }

    @Override
    public Integer updateRoleUser(RoleUserDTO roleUserDTO) {
        if (ObjectUtil.isNotNull(roleUserDTO)) {
            RoleUserEntity roleUser = new RoleUserEntity();
            BeanUtil.copyProperties(roleUserDTO, roleUser);
            return this.baseMapper.update(new RoleUserEntity(),
                    new UpdateWrapper<RoleUserEntity>()
                            .set("role_id", roleUser.getRoleId())
                            .eq("user_id", roleUser.getUserId()));
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteRoleUser(List<Long> ids) {
        if (ObjectUtil.isNotNull(ids) && ids.size() > 0) {
            return this.baseMapper.deleteBatchByUserIds(ids);
        } else {
            return 0;
        }
    }
}
